﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RESP_IIWaterAssessment
{
    public partial class WaterSupplyForm : Form
    {
        public WaterSupplyForm()
        {
            InitializeComponent();
        }

        int avpID, regID, canalID = 0;

        tb_watersupply[] decadaWaterSupply = new tb_watersupply[3];

        RESP_IIWaterAssessment.Entity.tb_watersupply[] decadaWaterSupply_s = new RESP_IIWaterAssessment.Entity.tb_watersupply[3];
        RESP_IIWaterAssessment.Entity.RESP_addDataContext resp_data = new RESP_IIWaterAssessment.Entity.RESP_addDataContext(DBClass.RESPContext.Connection);

        private void WaterSupplyForm_Load(object sender, EventArgs e)
        {
            cbRegion.SelectedIndexChanged -= cbRegion_SelectedIndexChanged;
            cbRegion.DataSource = UtilClass.FillCBRegions();
            cbRegion.DisplayMember = "Name";
            cbRegion.ValueMember = "ID";

            if (InformationClass.isAdmin)
            {
                cbRegion.SelectedIndex = -1;
                cbRegion.Text = string.Empty;
                cbRegion.SelectedIndexChanged += new EventHandler(cbRegion_SelectedIndexChanged);
            }
            else
            {
                cbRegion.Enabled = false;
                regID = InformationClass.regionID;
                cbRegion.SelectedIndexChanged += new EventHandler(cbRegion_SelectedIndexChanged);
                cbRegion.SelectedValue = regID;
                cbRegion_SelectedIndexChanged(cbRegion, null);
                cbAVP.Enabled = true;
                cbAVP.Text = string.Empty;
                //toolStrip1.Visible = false;
            }

            string[] month;
            if (InformationClass.Lang)
                month = new string[] { "Январь", "Февраль", "Март", "Апрель", "Май", "Июнь", "Июль", "Август", "Сентябрь", "Октябрь", "Ноябрь", "Декабрь" };
            else
                month = new string[] { "Январ", "Феврал", "Март", "Апрел", "Май", "Июн", "Июл", "Август", "Сентябр", "Октябр", "Ноябр", "Декабр" };

            for (int i = 1; i <= 12; i++)
            {
                var m = new
                {
                    ID = i,
                    Name = month[i - 1]
                };
                cbMonth.Items.Add(m);
            }
            cbMonth.DisplayMember = "Name";
            cbMonth.ValueMember = "ID";

            for (int i = 1991; i < 2100; i++)
            {
                cbYear.Items.Add(i);
            }
            cbMonth.SelectedIndex = cbMonth.FindStringExact(month[DateTime.Now.Month - 1]);
            cbYear.SelectedIndex = cbYear.FindStringExact(DateTime.Now.Year.ToString());

            cbCanal.Enabled = false;
        }

        private void cbRegion_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbRegion.SelectedIndex >= 0)
            {
                cbAVP.Enabled = true;
                regID = Convert.ToInt32(cbRegion.SelectedValue);
                cbAVP.SelectedIndexChanged -= cbAVP_SelectedIndexChanged;
                cbAVP.DataSource = UtilClass.FillAVP(regID);
                cbAVP.ValueMember = "ID";
                cbAVP.DisplayMember = "Name";
                cbAVP.SelectedIndex = -1;
                cbAVP.Text = string.Empty;
                cbAVP.SelectedIndexChanged += new EventHandler(cbAVP_SelectedIndexChanged);
            }
        }

        private void cbAVP_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbAVP.SelectedIndex >= 0)
            {
                avpID = Convert.ToInt32(cbAVP.SelectedValue);
                cbCanal.Enabled = true;
                cbCanal.SelectedIndexChanged -= cbCanal_SelectedIndexChanged;
                cbCanal.DataSource = DBClass.Canals
                    .Where(wh => wh.AVPID == avpID)
                    .Select(se => new { ID = se.ID, Name = se.Name })
                    .OrderBy(o => o.Name)
                    .ToArray();
                cbCanal.DisplayMember = "Name";
                cbCanal.ValueMember = "ID";
                cbCanal.SelectedIndex = -1;
                cbCanal.Text = string.Empty;
                cbCanal.SelectedIndexChanged += new EventHandler(cbCanal_SelectedIndexChanged);
            }
        }

        private void cbCanal_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbCanal.SelectedIndex >= 0)
            {
                canalID = Convert.ToInt32(cbCanal.SelectedValue);
                int m = cbMonth.SelectedIndex + 1;
                int y = Convert.ToInt32(cbYear.Text);
                ShowWaterSupply(canalID, m, y);
            }
        }

        private void ShowWaterSupply(int canalID, int m, int y)
        {
            for (int i = 0; i < decadaWaterSupply.Length; i++)
            {
                decadaWaterSupply[i] = DBClass.WaterSupplys
                    .Where(wh => wh.CanalID == canalID && wh.Date == new DateTime(y, m, (10 * i + 1)))
                    .SingleOrDefault();
                decadaWaterSupply_s[i] = resp_data.tb_watersupplies
                    .Where(wh => wh.CanalID == canalID && wh.Date == new DateTime(y, m, (10 * i + 1)))
                    .SingleOrDefault();
            } 
            if (decadaWaterSupply_s[0] != null)
            {
                tbPlan1.Text = decadaWaterSupply_s[0].WaterPlan.Value.ToString();
                tbLimit1.Text = decadaWaterSupply_s[0].WaterLimit.Value.ToString();
                tbFact1.Text = decadaWaterSupply_s[0].WaterFact.Value.ToString();
            }
            else
            {
                tbFact1.Text = tbLimit1.Text = tbPlan1.Text = string.Empty;
            }

            if (decadaWaterSupply[1] != null)
            {
                tbPlan2.Text = decadaWaterSupply_s[1].WaterPlan.Value.ToString();
                tbLimit2.Text = decadaWaterSupply_s[1].WaterLimit.Value.ToString();
                tbFact2.Text = decadaWaterSupply_s[1].WaterFact.Value.ToString();
            }
            else
            {
                tbFact2.Text = tbLimit2.Text = tbPlan2.Text = string.Empty;
            }

            if (decadaWaterSupply[2] != null)
            {
                tbPlan3.Text = decadaWaterSupply_s[2].WaterPlan.Value.ToString();
                tbLimit3.Text = decadaWaterSupply_s[2].WaterLimit.Value.ToString();
                tbFact3.Text = decadaWaterSupply_s[2].WaterFact.Value.ToString();
            }
            else
            {
                tbFact3.Text = tbLimit3.Text = tbPlan3.Text = string.Empty;
            }
        }

        private void cbYear_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (canalID > 0)
            {
                int m = cbMonth.SelectedIndex + 1;
                int y = Convert.ToInt32(cbYear.Text);
                ShowWaterSupply(canalID, m, y);
            }
        }

        private void cbMonth_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (canalID > 0)
            {
                int m = cbMonth.SelectedIndex + 1;
                int y = Convert.ToInt32(cbYear.Text);
                ShowWaterSupply(canalID, m, y);
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            decimal[] limit = new decimal[3], plan = new decimal[3], fact = new decimal[3];

            int month = cbMonth.SelectedIndex + 1;
            int year = Convert.ToInt32(cbYear.Text);
            int dec;
            
            if (!String.IsNullOrEmpty(tbPlan1.Text) || !String.IsNullOrEmpty(tbLimit1.Text) || !String.IsNullOrEmpty(tbFact1.Text))
            {
                dec = 0;
                tbFact1.Text.GetDecimalOrDefault(out fact[dec]);
                tbLimit1.Text.GetDecimalOrDefault(out limit[dec]);
                tbPlan1.Text.GetDecimalOrDefault( out plan[dec]);
                //decadaWaterSupply_s[dec] = new RESP_IIWaterAssessment.Entity.tb_watersupply();
                if (decadaWaterSupply[dec] != null)
                {
                    decadaWaterSupply[dec].WaterFact = fact[dec];
                    decadaWaterSupply[dec].WaterLimit = limit[dec];
                    decadaWaterSupply[dec].WaterPlan = plan[dec];
                    decadaWaterSupply_s[dec].WaterFact = fact[dec];
                    decadaWaterSupply_s[dec].WaterLimit = limit[dec];
                    decadaWaterSupply_s[dec].WaterPlan = plan[dec];
                }
                else
                {
                    decadaWaterSupply_s[dec] = new RESP_IIWaterAssessment.Entity.tb_watersupply()
                    {
                        CanalID = canalID,
                        Date = new DateTime(year, month, (10 * dec + 1)),
                        WaterFact = fact[dec],
                        WaterLimit = limit[dec],
                        WaterPlan = plan[dec]
                    };
                    resp_data.tb_watersupplies.InsertOnSubmit(decadaWaterSupply_s[dec]);
                    //DBClass.WaterSupplys.InsertOnSubmit(decadaWaterSupply[dec]);
                }
                
            }

            if (!String.IsNullOrEmpty(tbPlan2.Text) || !String.IsNullOrEmpty(tbLimit2.Text) || !String.IsNullOrEmpty(tbFact2.Text))
            {
                dec = 1;
                tbFact2.Text.GetDecimalOrDefault( out fact[dec]);
                tbLimit2.Text.GetDecimalOrDefault( out limit[dec]);
                tbPlan2.Text.GetDecimalOrDefault( out plan[dec]);

                if (decadaWaterSupply_s[dec] != null)
                {
                    decadaWaterSupply_s[dec].WaterFact = fact[dec];
                    decadaWaterSupply_s[dec].WaterLimit = limit[dec];
                    decadaWaterSupply_s[dec].WaterPlan = plan[dec];
                }
                else
                {
                    decadaWaterSupply_s[dec] = new RESP_IIWaterAssessment.Entity.tb_watersupply()
                    {
                        CanalID = canalID,
                        Date = new DateTime(year, month, (10 * dec + 1)),
                        WaterFact = fact[dec],
                        WaterLimit = limit[dec],
                        WaterPlan = plan[dec]
                    };
                    //DBClass.WaterSupplys.InsertOnSubmit(decadaWaterSupply[dec]);
                    resp_data.tb_watersupplies.InsertOnSubmit(decadaWaterSupply_s[dec]);
                }
            }

            if (!String.IsNullOrEmpty(tbPlan3.Text) || !String.IsNullOrEmpty(tbLimit3.Text) || !String.IsNullOrEmpty(tbFact3.Text))
            {
                dec = 2;
                tbFact3.Text.GetDecimalOrDefault( out fact[dec]);
                tbLimit3.Text.GetDecimalOrDefault( out limit[dec]);
                tbPlan3.Text.GetDecimalOrDefault( out plan[dec]);

                if (decadaWaterSupply_s[dec] != null)
                {
                    decadaWaterSupply_s[dec].WaterFact = fact[dec];
                    decadaWaterSupply_s[dec].WaterLimit = limit[dec];
                    decadaWaterSupply_s[dec].WaterPlan = plan[dec];
                }
                else
                {
                    decadaWaterSupply_s[dec] = new RESP_IIWaterAssessment.Entity.tb_watersupply()
                    {
                        CanalID = canalID,
                        Date = new DateTime(year, month, (10 * dec + 1)),
                        WaterFact = fact[dec],
                        WaterLimit = limit[dec],
                        WaterPlan = plan[dec]
                    };
                    //DBClass.WaterSupplys.InsertOnSubmit(decadaWaterSupply[dec]);
                    resp_data.tb_watersupplies.InsertOnSubmit(decadaWaterSupply_s[dec]);
                }
            }

            try
            {
                //DBClass.RESPContext.SubmitChanges();
                resp_data.SubmitChanges();
                MessageBox.Show(msgClass.msgSubmitChanges, msgClass.msgAchtung);
            }
            catch
            {
                MessageBox.Show(msgClass.msgError, msgClass.msgAchtung);
            }
        }
    }
}
